package configuration;

import java.io.*;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
import org.w3c.dom.*;


 /**
  * this class create and initialize db
  * @author ran
  *
  */
public class CreatXMLFile {
	public void createDbAndHibernateXml(String dbName,String userName , String password) {
		try {
		//create database
		java.sql.Connection Conn = DriverManager.getConnection
		("jdbc:mysql://localhost/?user="+userName+"&password="+password); 
		Statement s = Conn.createStatement();
		
		s.executeUpdate("CREATE DATABASE " + dbName);
		} catch (SQLException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		//create hibernate.cfg.xml configuration file
		String att[]={"connection.url","connection.driver_class","connection.username",
				"connection.password","connection.autocommit","dialect","hbm2ddl.auto","hibernate.jdbc.batch_size","show_sql"};
		String text[] = {"jdbc:mysql://localhost:3306/"+dbName,"com.mysql.jdbc.Driver",userName,password,
				"true","org.hibernate.dialect.MySQLDialect","update","0","true"};
		try {
			DocumentBuilderFactory documentBuilderFactory = 
				DocumentBuilderFactory.newInstance();
			DocumentBuilder documentBuilder;
			documentBuilder = documentBuilderFactory.newDocumentBuilder();
			//create document
			Document document = documentBuilder.newDocument();
			//create root
			Element rootElement = document.createElement("hibernate-configuration");
			document.appendChild(rootElement);
			//create other elements
			Element root2em = document.createElement("session-factory");
			for(int i=0;i<9;i++){
				Element em = document.createElement("property");
				em.setAttribute("name", att[i]);
				em.appendChild(document.createTextNode(text[i]));
				root2em.appendChild(em);
			}
			Element em8 = document.createElement("mapping");
			em8.setAttribute("resource", "TagedFiles.hbm.xml");
			root2em.appendChild(em8);
		
			Element em9 = document.createElement("mapping");
			em9.setAttribute("resource", "PlugIns.hbm.xml");
			root2em.appendChild(em9);
		
			rootElement.appendChild(root2em);
			//create the DTD redirection and encoding settings
			DOMSource domSource = new DOMSource(document);
			//open the outputstream
			StreamResult streamResult = new StreamResult(new File("C:\\Program Files\\project\\ClientService\\hibernate.cfg.xml"));
			System.out.println("ascasfcadfsdfsdfsdgsdfg");
			TransformerFactory tf = TransformerFactory.newInstance();
			Transformer serializer;
			serializer = tf.newTransformer();
			serializer.setOutputProperty(OutputKeys.ENCODING,"UTF-8");
			serializer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, "-//Hibernate/Hibernate Configuration DTD 3.0//EN");
			serializer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd");
			serializer.setOutputProperty(OutputKeys.INDENT,"yes");
			//write all the data to file
			serializer.transform(domSource, streamResult); 
		} catch (TransformerConfigurationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
		} catch (TransformerException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
		}catch (ParserConfigurationException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
		}
			

	}


}
